<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
    public function _initialize(){
		// 权限判断，Index控制器的login方法不需要登录，其他的必须登录后才可以浏览
    	$admin = $_SESSION['admin'];
    	if(!in_array(CONTROLLER_NAME.ACTION_NAME, array("Indexlogin","Indexindex"))){
    		if (empty($admin)){
    			$this -> error('请登陆后操作!', U('Index/login'));
    			exit;
    		}
    		if ($admin['role_name'] != "超级管理员" ){
    			$method = CONTROLLER_NAME.ACTION_NAME;
    			$role_area = $admin['role_area'];
    			if (!in_array($method, $role_area)){
    				$this->error("对不起，您没有访问该功能的权限", U('Index/login'));
    			}
    			$this->assign('role_area', $role_area);
    		}
    		
    		// _开头的函数为内部函数，不能直接访问
    		if(substr(ACTION_NAME,0,1) == '_'){
    			$this -> error('访问地址错误！', U('Index/index'));
    		}
    		
    		// 从数据库读取配置参数
    		$config = M('config') -> select();
    		foreach($config as $v){
    			$key = '_'.$v['name'];
    			$this -> $key = unserialize($v['value']);
    			$_CFG[$v['name']] = $this -> $key;
    		}
    		$this -> assign('_CFG', $_CFG);
    		$GLOBALS['_CFG'] = $_CFG;
    	}
    }
	
	// 后台首界面
	public function welcome(){
		
		$this -> assign('info', $info);
		$this -> display();
	}

	
	// 通用简单列表方法
	protected function _list($table, $where= null, $order = null, $alias = null, $field = null, $join = null, $pageSize = 25){
		$list = $this -> _get_list($table, $where, $order, $alias, $field, $join, $pageSize);
		$this -> assign('list', $list);
		$this -> assign('page', $this -> data['page']);
		$this -> display();
	}
	
	// 获得一个列表,返回而不输出
	protected function _get_list($table, $where= null, $order = null, $alias = null, $field = null, $join = null, $pageSize = 25){
		$model = M($table);
		$count = $model -> where($where) -> count();
		$page = new \Think\Page($count, $pageSize);
		if(!$order){
			$order = "id desc";
		}
		$list = $model 
		-> alias($alias)
		-> field($field) 
		-> join($join)
		-> where($where) 
		-> limit($page -> firstRow . ',' . $page -> listRows ) 
		-> order($order) 
		-> select();
		
		// 将数据保存到成员变量
		$this -> data = array(
			'list' => $list,
			'page' => $page -> show(),
			'count' => $count
		);
		
		return $list;
	}
	
	// 通用编辑方法,根据POST自动增加或者修改
	protected function _edit($table, $url = null){
		$model = M($table);
		
		$id = intval($_GET['id']);
		if($id>0){
			$info = $model -> find($id);
			if(!$info)
				die('信息不存在');
			
			$this -> assign('info', $info);
		}
		if(IS_POST){
			if(!$url)
				$url = U('index');
			if($id>0){
				$_POST['id'] = $id;
				$model -> save($_POST);
				$this -> success('操作成功！', $url);
				exit;
			}else{
				$model -> add($_POST);
				$this -> success('添加成功！', $url);
				exit;
			}
		}
		
		$this -> display();
	}
	
	// 通用删除
	protected function _del($table,$id){
		if (is_array($id)){
			foreach ($id as $new_id){
				$new_id = intval($new_id);
				if($new_id>0){
					$new_ids[] = $new_id;
				}
			}
			if(count($new_ids)>0){
				$where['id'] = array('in', implode(',', $new_ids));
			}
		}else {
			$id = intval($id);
			if($id>0){
				$where['id'] = $id;
			}
		}
		if(!empty($where) && !empty($table)){
			M($table) ->where($where)-> delete();
		}
	}
	
	
	// 上传图片
	public function upload(){
		if(!empty($_GET['url']))
			$this -> assign('url', $_GET['url']);
		if(IS_POST){
			
			if($_GET['field'])
				$field = $_GET['field'];
			if(empty($field))
				$field = 'file';
			
			if($_FILES[$field]['size'] < 1 && $_FILES[$field]['size']>0){
				$this -> assign('errmsg', '上传错误！');
			}else{
				$ext = _get_ext($_FILES[$field]['name']);
				$new_name = _get_new_name($ext, 'images');
				if(move_uploaded_file($_FILES[$field]['tmp_name'], $new_name['fullname'])){
					$this -> assign('url', $new_name['fullname']);
				}else
					$this -> assign('errmsg', '文件保存错误！');
				
				
			}
		}
		C('LAYOUT_ON', false);
		$this -> display('Base/upload');
	}
	
	
}?>